import QtQuick 2.9
        import QtQuick.Controls 2.5
        import QtQuick.Templates 2.12 as T
        import QtQuick.Window 2.2



 Rectangle{
    property bool  isMaximized: false
    Connections{
        target: page
        onVisibilityChanged: {//解决Qt窗口最大化的时候最小化，再恢复窗口变为普通窗口的bug
            if(isMaximized && visibility === 2) {
                mainWindow.showMaximized()
            }
        }
    }
    MouseArea{
        anchors.fill: parent
        acceptedButtons: Qt.LeftButton //只处理鼠标左键
        property bool   isDoubleClicked:false
        property point clickPos: "0,0"
         onPressed:
         {
             isDoubleClicked = false;
             clickPos = Qt.point(mouse.x,mouse.y)
         }
         onPositionChanged: {
             if(!isDoubleClicked && pressed && mainWindow.visibility !== Window.Maximized && mainWindow.visibility !== Window.FullScreen) {
                 var delta = Qt.point(mouse.x-clickPos.x, mouse.y-clickPos.y)
                 mainWindow.x += delta.x
                 mainWindow.y += delta.y
             }
             if(mainWindow.visibility === Window.Maximized && pressed && !isDoubleClicked)
             {
                 isMaximized = false;
                 mainWindow.showNormal();
<!--                 normBtnBg.source  = "qrc:/res/maximinze_btn.png"-->
             }
         }
         onDoubleClicked :
         {
             isDoubleClicked = true; // 这个时候一定不能响应onPositionChanged不然会一直置顶。
             if(isMaximized){
                 isMaximized = false;
                 mainWindow.showNormal();
<!--                 normBtnBg.source  = "qrc:/res/maximinze_btn.png"-->
             }else{
                 isMaximized = true;
                 mainWindow.showMaximized();
<!--                 normBtnBg.source = "qrc:/res/norm_btn.png"-->
             }
         }
     }

    Button{
        id:closeBtn
        anchors.right: parent.right
        width: parent.height
        height: parent.height - 1
        hoverEnabled : true
        background: Rectangle{
            color: closeBtn.hovered ? (closeBtn.pressed ? "#DE0000" : "#F91515") : "transparent"
            Image{
                id: closeBtnImg
                width: parent.height * 0.3; height: parent.height * 0.3
                anchors.centerIn: parent
<!--                source: "qrc:/res/close_btn.png"-->
            }
        }
        onClicked: {
            mainWindow.close()
        }
    }
    Button{
        id: normBtn
        anchors.right: closeBtn.left
        width: parent.height
        height: parent.height - 1
        hoverEnabled : true
        background: Rectangle{
            color: normBtn.hovered ? (normBtn.pressed ? "#4E4E4E" : "#666666") : "transparent"
            //color: "black"
            Image{
                id: normBtnBg
                width: parent.height * 0.3; height: parent.height * 0.3
                anchors.centerIn: parent
<!--                source: "qrc:/res/maximinze_btn.png"-->
            }
        }
        onClicked:{
            if(isMaximized){
                isMaximized = false;
                mainWindow.showNormal();
<!--                normBtnBg.source = "qrc:/res/maximinze_btn.png"-->
            }else{
                isMaximized = true;
                mainWindow.showMaximized();
<!--                normBtnBg.source = "qrc:/res/norm_btn.png"-->
            }
        }
    }
    Button{
        id: minBtn
        anchors.right: normBtn.left
        width: parent.height
        height: parent.height - 1
        hoverEnabled : true
        background: Rectangle{
            color: minBtn.hovered ? (minBtn.pressed ? "#4E4E4E" : "#666666") : "transparent"
            Image{
                width: parent.height * 0.3; height: parent.height * 0.3
                anchors.centerIn: parent
<!--                source: "qrc:/res/min_btn.png"-->
            }
        }
        onClicked:{
            mainWindow.showMinimized();
        }
    }
}
